package zz.app.yzjproject

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import java.util.concurrent.ArrayBlockingQueue


/**
 * 由作者 zzq 于 2017/9/24 16:23 创建
 */
class DegreeLineChartView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private val mPaint=Paint()
    val mDegreeData=ArrayBlockingQueue<DegreeData>(250)
    private val mXAsixLMargin=120f
    private val mXAsixRMargin=50f
    private val mYScaleStart=50f
    private val mStrokeWidth =5f
    private val mTextSize=50f
    private val mXAsixTextStart=20f
    private val mDrawStartY=80f
    private val mLabelWidth=500f
    private val mLabelEnd=20f
    private val oriColor=Color.RED
    private val kalColor=Color.YELLOW
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawbkgnd()
        canvas.drawaxis()
        canvas.drawdata()
        canvas.drawlabel()

    }
    private fun Canvas.drawaxis() {
        mPaint.reset()
        mPaint.color=Color.BLACK
        mPaint.strokeWidth= mStrokeWidth
        mPaint.textSize=mTextSize
        drawLine(mXAsixLMargin,(mDrawStartY+height.toFloat())/2,width.toFloat()-mXAsixRMargin,(mDrawStartY+height.toFloat())/2,mPaint)
        drawLine(mXAsixLMargin,mDrawStartY,mXAsixLMargin,height.toFloat(),mPaint)

        for(i in 0..8) {
            mPaint.style=Paint.Style.FILL
            mPaint.color = Color.DKGRAY
            drawText(String.format("%4d",120 - 30 * i), mXAsixTextStart,mDrawStartY+ mYScaleStart+i * (height.toFloat()-mDrawStartY - 2 * mYScaleStart) / 8+mTextSize/2, mPaint)
            if (i != 4) {
                mPaint.style=Paint.Style.STROKE
                mPaint.color = Color.LTGRAY
                mPaint.pathEffect=DashPathEffect(floatArrayOf(10f,10f),0f)
                val path= Path()
                path.moveTo(mXAsixLMargin,mDrawStartY+ mYScaleStart+i * (height.toFloat()-mDrawStartY - 2 * mYScaleStart) / 8)
                path.lineTo(width.toFloat() - mXAsixRMargin,mDrawStartY+ mYScaleStart+i * (height.toFloat()-mDrawStartY - 2 * mYScaleStart) / 8)
                drawPath(path,mPaint)
            }
        }
        mPaint.pathEffect=null

    }
    private fun Canvas.drawRect2(x: Float, y: Float, x2: Float, y2: Float, p: Paint) {
        drawLine(x,y,x,y2,p)
        drawLine(x,y,x2,y,p)
        drawLine(x,y2,x2,y2,p)
        drawLine(x2,y,x2,y2,p)
    }
    private fun Canvas.drawdata() {
        mPaint.reset()
        mPaint.strokeWidth=mStrokeWidth
        for (i in 0..249) {
            val item1 = mDegreeData.elementAtOrNull(i)
            val item2 = mDegreeData.elementAtOrNull(i + 1)
            if (item1 != null && item2 != null) {
                var i1 = item1.ori
                var i2 = item2.ori
                mPaint.color = oriColor
                drawLine((width.toFloat() - mXAsixLMargin - mXAsixRMargin) * i / 250+mXAsixLMargin,
                        (mDrawStartY + mYScaleStart - (i1 - 120) * (height.toFloat() - mDrawStartY - 2*mYScaleStart) / 240),
                        (width.toFloat() - mXAsixLMargin - mXAsixRMargin) * (i + 1) / 250+mXAsixLMargin,
                        (mDrawStartY + mYScaleStart - (i2 - 120) * (height.toFloat() - mDrawStartY - 2*mYScaleStart) / 240), mPaint)

                i1 = item1.kal
                i2 = item2.kal
                mPaint.color = kalColor
                drawLine((width.toFloat() - mXAsixLMargin - mXAsixRMargin) * i / 250+mXAsixLMargin,
                        (mDrawStartY + mYScaleStart - (i1 - 120) * (height.toFloat() - mDrawStartY - 2*mYScaleStart) / 240),
                        (width.toFloat() - mXAsixLMargin - mXAsixRMargin) * (i + 1) / 250+mXAsixLMargin,
                        (mDrawStartY + mYScaleStart - (i2  - 120) * (height.toFloat() - mDrawStartY - 2*mYScaleStart) / 240), mPaint)

            }
        }
    }
    private fun Canvas.drawbkgnd() {
        mPaint.reset()
        mPaint.color=Color.argb(255,244,244,244)
        drawRect(0f,0f,width.toFloat(),height.toFloat(),mPaint)
    }
    private fun Canvas.drawlabel() {
        mPaint.reset()
        mPaint.strokeWidth=mStrokeWidth
        mPaint.textSize=mTextSize
        mPaint.color = Color.WHITE
        drawRect(width.toFloat() - mLabelWidth - mLabelEnd, 0f, width.toFloat() - mLabelEnd, mDrawStartY, mPaint)
        mPaint.color = Color.BLACK
        drawRect2(width.toFloat() - mLabelWidth - mLabelEnd, 0f, width.toFloat() - mLabelEnd, mDrawStartY, mPaint)
        drawText("原始", width.toFloat() - mLabelWidth - mLabelEnd + 120f, mDrawStartY / 2 + mTextSize / 2, mPaint)
        drawText("滤波", width.toFloat() - mLabelWidth - mLabelEnd + 370f, mDrawStartY / 2 + mTextSize / 2, mPaint)
        mPaint.color = oriColor
        drawLine(width.toFloat() - mLabelWidth - mLabelEnd + 10f, mDrawStartY / 2, width.toFloat() - mLabelWidth - mLabelEnd + 110f, mDrawStartY / 2, mPaint)
        mPaint.color = kalColor
        drawLine(width.toFloat() - mLabelWidth - mLabelEnd + 260f, mDrawStartY / 2, width.toFloat() - mLabelWidth - mLabelEnd + 360f, mDrawStartY / 2, mPaint)
    }
}